﻿<#
    1. 本脚本使用方式，在windows powershell中运行。ps: 右键选择powershell运行即可
    2. 本脚本的case场景：
        Scroll map/在500ms内拖动地图100mm循环10次
#>

chcp 65001

# - - - - - - - - - - - - constant - - - - - - - - - - - - - - - -
$app_name = "com.dayou.map/com.dayou.map.MainActivity"

$log_file = "./在500ms内拖动地图100mm循环10次.log"

# 循环次数
$ctimes = 10
# 拖动距离
$move_distance = 100
# 拖动时间
$drag_duration = 500
# 拖动模式
$drag_mode = "xy"
# - - - - - - - - - - basic function - - - - - - - - - - - - - -
# 重新定义print内容
function print($_content) {
    $cur_time = Get-Date -Format "%y-%M-%d %h:%m:%s"
    Write-Host $cur_time $_content
    $all_content = "$cur_time, $_content"
    Write-Output  $all_content >> $log_file
}
# - - - - - - - - - - basic function - - - - - - - - - - - - - -

# 检查adb是否正常安装
function check_adb_is_installed(){
    print "正在检测Adb命令工具是否安装中..."
    $adb_result=adb version
    if (-not $adb_result){
        Throw "Adb 电脑端没有找到安装路径，请检查 
        @1是否安装Adb工具，下载地址：https://dl.google.com/android/repository/platform-tools-latest-windows.zip 
        @2 系统环境变量是否正确添加
        "
    } else {
        if($adb_result.Count -gt 1 -and $adb_result[2].contains("Installed")) {
            $adb_path=$adb_result[2] -split "as"
            print @("已检查到Adb工具本地已正常安装！路径为-》", $adb_path[-1].Trim())
        } 
    }
}

# 查看adb设备是否连接上
function adb_service_connected() {
    print "正在查验车机设备是否连接上..."
    $services = adb devices | select -Skip 1
    if($services.Count -gt 1){
        for($i=0;$i -lt $services.Count - 1; $i+=1) {
            $sn=$services[$i].split()[0].trim()
            print @("设备连接正常，设为sn号为：",$sn)
        }
    } else {
        Throw "设备没有正常接入，请检查安卓系统是否开启了adb调试，步骤为：设置-》关于本机-》软件版本(连续点击5次)-》ADB模式切换(点击1次)"
    }
    # 返回值
    $sn
}

function drag_map_move($_params){
    <#
        本函数为拖动地图的操作
        $times: 循环次数
        $duration: 动作持续的时间
        $move_size: 拖动的距离
        $move_mode：拖动的方向，x表示水平，y表示竖直，xy表示混合
    #>
    $times, $duration, $move_size, $move_mode = $_params
    if($move_mode -eq "x"){
        $move_action = @{
            sx=$screen_size_x / 2;
            sy=$screen_size_y / 2;
            ex=$screen_size_x / 2 + $move_size;
            ey=$screen_size_y / 2
        }
    }
    elseif ($move_mode -eq "y"){
        $move_action = @{
            sx=$screen_size_x / 2;
            sy=$screen_size_y / 2;
            ex=$screen_size_x / 2;
            ey=$screen_size_y / 2 + $move_size
        }
    }
    elseif ($move_mode -eq "xy") {
        $move_action = @{
            sx=$screen_size_x / 2;
            sy=$screen_size_y / 2;
            ex=[int]($screen_size_x / 2 + 0.6 * $move_size);
            ey=[int]($screen_size_y / 2 + 0.8 * $move_size)
        }
    }
    # 循环times次
    for($_i=0;$_i -lt $times; $_i+=1) {
        Write-Host "adb -s $device_sn shell input touchscreen -d 0 draganddrop $move_action.sx $move_action.sy $move_action.ex $move_action.ey $duration"
        # 鼠标点击屏幕中心让屏幕地图窗口激活
        adb -s $device_sn shell input mouse tap 960 360
        #adb -s $device_sn shell input touchscreen -d 0 draganddrop $move_action.sx $move_action.sy $move_action.ex $move_action.ey $duration
        adb -s $device_sn shell input touchscreen -d 0 swipe $move_action.sx $move_action.sy $move_action.ex $move_action.ey $duration        
        #Start-Sleep 1
    }
}

# 检查adb工具
check_adb_is_installed
# 安卓设备是否正常连接
$global:device_sn = adb_service_connected

# 以root身份操作adb
adb root
# 初始化操作
adb -s $device_sn shell am force-stop $app_name.Split("/")[0]

# 获取屏幕分辨率
$size_string = $(adb -s $device_sn shell wm size).split()[2]
$global:screen_size_x = [int]$size_string.Split("x")[0]
$global:screen_size_y = [int]$size_string.Split("x")[1]


# 启动APP 
adb -s $device_sn shell am start -n $app_name
# 等待界面响应
Start-Sleep 15


# 500ms拖动地图100mm循环10次
drag_map_move($ctimes, $drag_duration, $move_distance, $drag_mode)

# 停止APP
adb -s $device_sn shell am force-stop $app_name.Split("/")[0]


#cmd /c pause